import throttle from '../../../../utils/throttle.js';
import { concernUser } from '../../../../utils/tools';
import Http from '../../../../utils/http.js';
const http = new Http();
const app = getApp();

// 是否正在修改input内的值
let changeInputValue = false;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        isFocus: true,
        isCancel: true,
        // 关注列表
        concernList: [],
        // 输入框内容
        inputValue: '',
        // 搜索同事列表
        pageNum: 0,
        loading: true,
        loadingAll: false,
        // 搜索列表
        matchColleague: [],
        // 正在搜索
        searching: false,
    },
    // 查看他人主页
    jumpOthersPage(e) {
        wx.navigateTo({
            url: `../../othersHomePage/othersHomePage?uid=${e.currentTarget.dataset.uid}`,
        });
    },
    // 返回上一页
    back() {
        wx.navigateBack({
            detal: 1,
        });
    },
    onLoad: function (options) {
        this.getMyConcernList();
    },
    // 监听input输入
    bindKeyInput: throttle(function (e) {
        console.log(e.detail.value);
        if (changeInputValue) return;
        this.setData(
            {
                inputValue: e.detail.value,
                pageNum: 0,
                loading: true,
                loadingAll: false,
                matchColleague: [],
            },
            () => {
                this.searchColleague();
            }
        );
    }, 1000),

    clear() {
        changeInputValue = true;
        this.setData({
            isCancel: true,
            inputValue: '',
        });
    },

    // focus事件
    focus() {
        changeInputValue = false;
        this.setData({
            isCancel: false,
            isFocus: true,
        });
    },

    blur() {
        this.setData({
            isFocus: false,
        });
    },
    // 获取关注列表
    getMyConcernList() {
        let _that = this;
        http.getRequest(
            `cjMap/getMyConcernList?authId=${app.globalData.authId}`
        ).then((res) => {
            if (res.status == 0) {
                console.log(res.data);
                _that.setData({
                    concernList: res.data,
                });
            }
        });
    },
    // 关注列表取消关注
    cancelConcernBtn(e) {
        let _that = this;
        let uid = e.currentTarget.dataset.uid;
        wx.showModal({
            title: '提示：',
            content: '确认要取消关注吗？',
            success: function (res) {
                if (res.confirm) {
                    concernUser(uid, '00', function () {
                        wx.showToast({
                            title: '已取消关注',
                            icon: 'none',
                            duration: 2000,
                        });
                        _that.getMyConcernList();
                        _that.refreshAttention();
                    });
                } else {
                    console.log('点击取消回调');
                }
            },
        });
    },
    // 搜索同事
    searchColleague() {
        let _that = this;
        if (!_that.data.loadingAll) {
            _that.setData({
                pageNum: _that.data.pageNum + 1,
                loading: true,
                loadingAll: false,
            });
            wx.request({
                url:
                    app.globalData.rootPath +
                    'cjMap/getInsideUserByRealNameOrPhone',
                data: {
                    authId: app.globalData.authId,
                    pageSize: 20,
                    pageNo: _that.data.pageNum,
                    realnameorphone: _that.data.inputValue,
                },
                success(res) {
                    if (res.data.status === '0') {
                        _that.setData({
                            loading: false,
                            matchColleague: res.data.data,
                        });

                        if (
                            res.data.pager.currentPage >=
                            res.data.pager.totalPages
                        ) {
                            _that.setData({
                                loadingAll: true,
                            });
                        }
                    } else {
                        _that.setData({
                            loadingAll: true,
                            loading: false,
                        });
                    }
                },
            });
        }
    },
    //关注按钮
    concernBtn(e) {
        let _that = this;
        let index = e.currentTarget.dataset.index;
        let uid = e.currentTarget.dataset.uid;
        (_that.data.matchColleague[index].concernflg =
            _that.data.matchColleague[index].concernflg == '00' ? '01' : '00'),
            _that.setData(
                {
                    matchColleague: _that.data.matchColleague,
                },
                () => {
                    concernUser(
                        uid,
                        _that.data.matchColleague[index].concernflg,
                        function () {
                            wx.showToast({
                                title:
                                    _that.data.matchColleague[index]
                                        .concernflg == '00'
                                        ? '已取消关注'
                                        : '已关注',
                                icon: 'none',
                                duration: 2000,
                            });
                            _that.getMyConcernList();
                            _that.refreshAttention();
                        }
                    );
                }
            );
    },
    // 刷新上个页面关注信息
    refreshAttention() {
        let _that = this;
        let pages = getCurrentPages(); //当前页面栈id
        let prevPage = pages[pages.length - 2]; //上一页面
        let prevPage2 = pages[pages.length - 3]; //上一页面
        prevPage.setData(
            {
                diaryArr: [],
                pageNum: 0,
            },
            () => {
                prevPage.getConcernUserDiaryList();
            }
        );
        prevPage.getminePageNum();
        prevPage2.getminePageNum();
    },
});
